<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item,index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{active:activeIndex === index}"
        @click="menuChange(index)"
      >
        <div class="role-info">
          <svg-icon :icon-class="activeIndex === index ? 'user-active':'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="editRole(item.roleId)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="deleteRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/sys/addRole')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="permission">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }} </div>
              <el-tree
                ref="roleTree"
                :data="item.children"
                show-checkbox
                default-expand-all
                node-key="id"
                :props="defaultProps"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="'成员列表(' + total +')'" name="member">
          <div class="user-wrapper">
            <el-table
              :data="userList"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getRoleList, getTreeList, getRolePermission, getRoleUser, deleteRole } from '@/api/system'
export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      activeIndex: 0,
      treeList: [],
      defaultProps: {
        label: 'title',
        disabled: () => true
      },
      activeName: 'permission',
      params: {
        pageSize: 10,
        page: 1
      },
      total: 0,
      userList: []
    }
  },
  async created() {
    // 这两个是异步任务
    await this.getRoleList()
    await this.getTreeList()
    // 等执行完上面的任务，再执行下面这个
    this.menuChange(0)
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const { data: res } = await getRoleList()
      this.roleList = res
    },
    // 菜单点击事件
    async menuChange(index) {
      this.activeIndex = index
      const roleId = this.roleList[index].roleId
      this.getRolePermission(roleId)
      this.getUserRole(roleId)
    },
    // 获取权限列表
    async getTreeList() {
      const { data: res } = await getTreeList()
      this.treeList = res
      // this.addTreeListDisabled(this.treeList)
    },
    // 递归调用添加禁用效果
    addTreeListDisabled(list) {
      // 与设置props属性效果相同，props的disabled一定要使用函数方式
      list.forEach(item => {
        item.disabled = true
        if (item.children) {
          this.addTreeListDisabled(item.children)
        }
      })
    },
    // 获取角色对应的权限
    async getRolePermission(id) {
      const { data: res } = await getRolePermission(id)
      const perms = res.perms
      const treeList = this.$refs.roleTree
      treeList.forEach((tree, index) => {
        tree.setCheckedKeys(perms[index])
      })
    },
    // 获取角色对应的成员列表
    async getUserRole(id) {
      const { data: res } = await getRoleUser(id, this.params)
      this.total = res.total
      this.userList = res.rows
    },
    editRole(id) {
      this.$router.push({
        path: '/sys/addRole',
        query: {
          id: id
        }
      })
    },
    deleteRole(id) {
      this.$confirm('确定要删除数据吗？', '提示').then(async () => {
        await deleteRole(id)
        this.$message.success('删除角色成功')
        this.getRoleList()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
